﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2020-20XX，                                        *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019/11/24 23:17:05
 *  文件描述: 状态机
 *****************************************************************************/

using UnityEngine;
using Util;

namespace StateModel
{
    public enum StateType
    {
        Idle,
        Move,//移动
        Attack,//攻击
        Over,//结束
        Drill,//创造
        Build,//建造
    }

    public class StateManage : ModelInfo
    {
        [HideInInspector] public Transform Root;
        public State[] AllState;
        public Mdic<StateType, State> dicAll = new Mdic<StateType, State>();
        public State curState;

        public override void InitModel(Controller controller)
        {
            base.InitModel(controller);
            Root = m_transform.FindNode("State");
            AllState = Root.GetComponentsInChildren<State>();
            if (AllState.Length <= 0) {
                curState = Root.FindNode("IdleState").GetOrAddComponent<IdleState>();
            }
            else {
                dicAll = new Mdic<StateType, State>();
                for (int i = 0; i < AllState.Length; i++) {
                    AllState[i].InitModel(controller);
                    dicAll.Add(AllState[i].stateType, AllState[i]);
                    if (AllState[i].stateType == StateType.Idle) {
                        curState = AllState[i];
                    }
                }
            }
        }

        public override void GetModel()
        {
            base.GetModel();
            for (int i = 0; i < AllState.Length; i++) {
                AllState[i].GetModel();
            }
        }

        public override void OnUpdata(float dt)
        {
            curState.OnUpdata(dt);
        }

        public void SwitchState(StateType type)
        {
            curState.End();
            curState = dicAll[type];
            curState.Str();
        }

        public T Fin<T>(StateType type) where T : State
        {
            return dicAll[type] as T;
        }
    }
}